/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.entity.BusBinding;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.NormalJms;
import han.bifang.gwt.client.common.entity.ServiceBus;
import han.bifang.gwt.client.common.entity.SoapBinding;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainContentConstants;
import han.bifang.gwt.client.domain.DomainMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class BusDetailSubContent extends Content {
	private DomainMessages cons;
	private BifangGWTServiceAsync service;
	private String busId = null;
	private GwtContext context;

	/**
	 * keep this url in case of modify
	 */
	private String url = null;

	private VerticalPanel p = null;

	private ServiceBus sc = null;

	private ArrayList bindings = null;

	public void onLoad() {

		VerticalPanel v = new VerticalPanel();
		v.setWidth("80%");
		v.setBorderWidth(0);

		HTML hh = new HTML(cons.busBasic());
		v.add(hh);
		Grid g = new Grid(5, 2);
		g.setCellPadding(1);
		g.setCellSpacing(1);
		g.setBorderWidth(1);

		ColumnFormatter ccf = g.getColumnFormatter();
		ccf.setWidth(0, "15%");
		ccf.setWidth(1, "45%");

		CellFormatter cff = g.getCellFormatter();
		cff.setStyleName(0, 0, "tablecol");
		cff.setStyleName(1, 0, "tablecol");
		cff.setStyleName(2, 0, "tablecol");
		cff.setStyleName(3, 0, "tablecol");
		cff.setStyleName(4, 0, "tablecol");

		g.setHTML(0, 0, cons.name());
		g.setHTML(1, 0, cons.code());
		g.setHTML(2, 0, cons.appServer());
		g.setHTML(3, 0, cons.serVersion());
		g.setHTML(4, 0, cons.desc());

		g.setHTML(0, 1, sc.getName());
		g.setHTML(1, 1, sc.getCode());
		g.setHTML(2, 1, sc.getAppServer());
		g.setHTML(3, 1, sc.getServerVersion());

		g.setHTML(4, 1, sc.getDescription());
		v.add(g);

		HorizontalPanel h1 = new HorizontalPanel();
		Button b = new Button(cons.modify());
		b.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				HashMap m = new HashMap();
				m.put("bus", sc);
				m.put("url", url);
				listener.onContentChanged(DomainContentConstants.BUS_DETAIL,
						DomainContentConstants.MODIFY_BUS1, m);

			}
		});

		Button b1 = new Button(cons.remove());
		b1.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				HashMap m = new HashMap();
				m.put("bus", sc);
				listener.onContentChanged(DomainContentConstants.BUS_DETAIL,
						DomainContentConstants.REMOVE_BUS, m);

			}
		});
		h1.add(b);
		h1.add(b1);

		if (PrivilegChecker.hasPrivilege(context, null))
			v.add(h1);

		p.add(v);

		VerticalPanel v1 = new VerticalPanel();
		v1.setWidth("80%");
		v1.setSpacing(10);

		hh = new HTML(cons.busBinding());
		v1.add(hh);

		
		for (int i = 0, j = bindings.size(); i < j; i++) {
			BusBinding cb = (BusBinding) bindings.get(i);
			if (cb.getType().equals(EntityConstants.BINDING_HTTP)) {
				VerticalPanel p1 = new VerticalPanel();
				p1.setWidth("100%");
				p1.setBorderWidth(0);
				HttpBinding hb = (HttpBinding) cb.getBinding();
				HTML h = new HTML();

				if (cb.getName().equals(EntityConstants.BUS_BINDING_HTTP)) {
					h.setHTML(cons.httpBinding());
					url = StringUtils.getIPPORTOfHttpBinding(hb.getUrl());
				} else if (cb.getName().equals(
						EntityConstants.BUS_BINDING_MONITOR)) {
					h.setHTML(cons.monitorBinding());

				} else if (cb.getName().equals(
						EntityConstants.BUS_BINDING_RETRY)) {
					h.setHTML(cons.retryBinding());

				} else if (cb.getName()
						.equals(EntityConstants.BUS_BINDING_SYNC)) {
					h.setHTML(cons.syncBinding());

				}else if (cb.getName()
						.equals(EntityConstants.BUS_BINDING_LOG_QUERY)) {
					h.setHTML(cons.logQueryBinding());

				}

				p1.add(h);
				Grid table = new Grid(3, 2);
				table.setWidth("100%");
				ColumnFormatter cf = table.getColumnFormatter();
				cf.setWidth(0, "20%");
				cf.setWidth(1, "50%");

				table.setCellPadding(1);
				table.setCellSpacing(1);
				table.setBorderWidth(1);

				CellFormatter ff = table.getCellFormatter();
				ff.setStyleName(0, 0, "tablecol");
				ff.setStyleName(1, 0, "tablecol");
				ff.setStyleName(2, 0, "tablecol");

				table.setHTML(0, 0, cons.name());
				table.setHTML(1, 0, cons.type());
				table.setHTML(2, 0, cons.url());

				table.setWidget(0, 1, h);
				table.setHTML(1, 1, cons.http());
				table.setHTML(2, 1, hb.getUrl());

				p1.add(table);
				v1.add(p1);
			} else if (cb.getType().equals(EntityConstants.BINDING_JMS)) {
				VerticalPanel p1 = new VerticalPanel();
				p1.setWidth("100%");
				p1.setBorderWidth(0);
				JmsBindingWrapper w = (JmsBindingWrapper) cb.getBinding();
				JmsBinding jb = w.getJmsBinding();
				NormalJms info = (NormalJms) w.getInfo();

				Grid table = new Grid(10, 2);
				table.setWidth("100%");
				ColumnFormatter cf = table.getColumnFormatter();
				cf.setWidth(0, "20%");
				cf.setWidth(1, "50%");

				table.setCellPadding(1);
				table.setCellSpacing(1);
				table.setBorderWidth(1);

				CellFormatter ff = table.getCellFormatter();
				ff.setStyleName(0, 0, "tablecol");
				ff.setStyleName(1, 0, "tablecol");
				ff.setStyleName(2, 0, "tablecol");
				ff.setStyleName(3, 0, "tablecol");
				ff.setStyleName(4, 0, "tablecol");
				ff.setStyleName(5, 0, "tablecol");
				ff.setStyleName(6, 0, "tablecol");
				ff.setStyleName(7, 0, "tablecol");
				ff.setStyleName(8, 0, "tablecol");
				ff.setStyleName(9, 0, "tablecol");

				table.setHTML(0, 0, cons.name());
				table.setHTML(1, 0, cons.type());
				table.setHTML(2, 0, cons.jmsServer());
				table.setHTML(3, 0, cons.jmsServerVersion());
				table.setHTML(4, 0, cons.jmsServerUrl());
				table.setHTML(5, 0, cons.jmsFac());
				table.setHTML(6, 0, cons.jmsDestination());
				table.setHTML(7, 0, cons.jmsType());
				table.setHTML(8, 0, cons.jmsUser());
				table.setHTML(9, 0, cons.jmsPass());

				HTML h = new HTML(cons.jmsBinding());
				p1.add(h);

				table.setWidget(0, 1, h);
				table.setHTML(1, 1, cons.jms());

				table.setHTML(2, 1, info.getServerUrl());
				table.setHTML(3, 1, jb.getServer());
				table.setHTML(4, 1, jb.getServerVersion());

				table.setHTML(5, 1, info.getFactoryUrl());
				table.setHTML(6, 1, info.getDestinationJndi());
				table.setHTML(7, 1, cons.jmsTypeQueue());
				// jmsUser = new TextBox();
				// jmsUser.setVisibleLength(32);
				// ;
				// jmsUser.setText(jb.getUser());
				table.setHTML(8, 1, "&nbsp;");
				// jmsPass = new PasswordTextBox();
				// jmsPass.setVisibleLength(32);

				// jmsPass.setText(jb.getPassword());
				table.setHTML(9, 1, "&nbsp;");

				p1.add(table);
				v1.add(p1);

			} else if (cb.getType().equals(EntityConstants.BINDING_SOAP)) {
				VerticalPanel p1 = new VerticalPanel();
				p1.setWidth("100%");
				p1.setBorderWidth(0);
				SoapBinding jb = (SoapBinding) cb.getBinding();

				Grid table = new Grid(4, 2);
				table.setWidth("100%");
				ColumnFormatter cf = table.getColumnFormatter();
				cf.setWidth(0, "20%");
				cf.setWidth(1, "50%");

				table.setCellPadding(1);
				table.setCellSpacing(1);
				table.setBorderWidth(1);

				CellFormatter ff = table.getCellFormatter();
				ff.setStyleName(0, 0, "tablecol");
				ff.setStyleName(1, 0, "tablecol");
				ff.setStyleName(2, 0, "tablecol");
				ff.setStyleName(3, 0, "tablecol");
				
				// ff.setStyleName(3, 0, "tablecol");
				// ff.setStyleName(4, 0, "tablecol");
				// ff.setStyleName(5, 0, "tablecol");

				table.setHTML(0, 0, cons.name());
				table.setHTML(1, 0, cons.type());
				table.setHTML(2, 0, cons.url());
				table.setHTML(3, 0, cons.soapVersion());
				// table.setHTML(3, 0, cons.namespace());
				// table.setHTML(4, 0, cons.wsdl());
				// table.setHTML(5, 0, cons.endpoint());

				HTML h = new HTML(cons.soapBinding());
				p1.add(h);

				table.setWidget(0, 1, h);
				table.setHTML(1, 1, cons.soap());
				table.setHTML(2, 1, jb.getUrl());
				table.setHTML(3, 1, jb.getSoapVersion());
				// table.setHTML(3, 1, jb.getNamespace());

				// table.setHTML(4, 1, jb.getWsdlUrl());
				// table.setHTML(5, 1,jb.getEndpointUrl());

				p1.add(table);
				v1.add(p1);

			}else if (cb.getType().equals(EntityConstants.BINDING_WEB_SERVICES)) {
				VerticalPanel p1 = new VerticalPanel();
				p1.setWidth("100%");
				p1.setBorderWidth(0);
				SoapBinding jb = (SoapBinding) cb.getBinding();

				Grid table = new Grid(4, 2);
				table.setWidth("100%");
				ColumnFormatter cf = table.getColumnFormatter();
				cf.setWidth(0, "20%");
				cf.setWidth(1, "50%");

				table.setCellPadding(1);
				table.setCellSpacing(1);
				table.setBorderWidth(1);

				CellFormatter ff = table.getCellFormatter();
				ff.setStyleName(0, 0, "tablecol");
				ff.setStyleName(1, 0, "tablecol");
				ff.setStyleName(2, 0, "tablecol");
				ff.setStyleName(3, 0, "tablecol");
				
				// ff.setStyleName(3, 0, "tablecol");
				// ff.setStyleName(4, 0, "tablecol");
				// ff.setStyleName(5, 0, "tablecol");

				table.setHTML(0, 0, cons.name());
				table.setHTML(1, 0, cons.type());
				table.setHTML(2, 0, cons.url());
				table.setHTML(3, 0, cons.soapVersion());
				// table.setHTML(3, 0, cons.namespace());
				// table.setHTML(4, 0, cons.wsdl());
				// table.setHTML(5, 0, cons.endpoint());

				HTML h = new HTML(cons.webservicesBinding());
				p1.add(h);

				table.setWidget(0, 1, h);
				table.setHTML(1, 1, cons.webservices());
				table.setHTML(2, 1, jb.getUrl());
				table.setHTML(3, 1, jb.getSoapVersion());
				// table.setHTML(3, 1, jb.getNamespace());

				// table.setHTML(4, 1, jb.getWsdlUrl());
				// table.setHTML(5, 1,jb.getEndpointUrl());

				p1.add(table);
				v1.add(p1);

			}

		}
		p.add(v1);

	}

	public BusDetailSubContent(DomainMessages cons,
			BifangGWTServiceAsync service, String busId, GwtContext context,
			ServiceBus sc, ArrayList bindings) {
		this.cons = cons;
		this.service = service;
		this.busId = busId;
		this.context = context;
		this.sc = sc;
		this.bindings = bindings;

		p = new VerticalPanel();
		p.setSpacing(25);
		p.setWidth("100%");
		initWidget(p);

	}

	public void onUnload() {
		this.sc = null;

	}

}
